import * as usingService from '../../services/using';
import {queryAssetType} from '../../services/assetType';
import {qryDepts,qryUserByDept} from '../../services/user';

export default {
  namespace: 'apply',
  state: {
    list: [],
    assetTypeTree:[],
    deptTree:[],
    roomList:[],
    userList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',    
    newKey : '',
    newKey1 : '',
    newKey2 : '',
    detailList:[],
    currentItem: {},
    detailModalType : 'create',
    detailModalVisible : false,
    detailItem:{},
    viewModalVisible:false,
    viewItem:{},
    viewDetailList:[],
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    changeDetailList(state, { payload }) {
      return { ...state, ...payload };
    },
    qryDeptsSuc(state, { payload: deptTree }) {
      return { ...state, deptTree };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey1 : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state,detailList:[], newKey1 : new Date().getTime() + '' , modalVisible: false, }
    },
    showApplyDetail (state, { payload }) {
        return { ...state, ...payload, newKey2 : new Date().getTime() + '' , detailModalVisible: true }
    },
    hideApplyDetail (state, { payload }) {
      return { ...state,  ...payload, newKey2 : new Date().getTime() + '' , detailModalVisible: false, }
    },
    showViewModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , viewModalVisible: true }
    },
    hideViewModal (state) {
      return { ...state, viewModalVisible: false, newKey : new Date().getTime() + '' }
    },
  },
  effects: {    
    *qryDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'qryDeptsSuc', payload: body.data });
    },
    *qryUserByDept({ payload },{ call, put }) {    
      const { body }  = yield call(qryUserByDept,payload);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(usingService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *createApply({ payload },{ call, put }){
      const { body } = yield call(usingService.create,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(usingService.qryById,payload);
      yield put({type: 'showViewModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},
      });
    },  
    *saveDetailList({ payload },{ call, put }){
      yield put({ type: 'changeDetailList' , payload});
      yield put({ type: 'hideApplyDetail' , payload:{detailItem:{}}});
    },  

    *edit({ payload },{ call, put }){
      const { body } = yield call(usingService.edit,payload);
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(usingService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/using/apply') {
          dispatch({ type: 'qryDepts' });
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
